
clear 
**************************

use "${build}/iv_fin_lehd_w_comp_weights.dta", clear
replace black = 0 if black_miss == 1
replace fem = 0 if fem_miss == 1

*Create datasets we will populate with results
cap frame change default
cap frame drop results 
frame create results strL(label) b_evict se_evict n_obs y_base_mean y_base_sd y_base_n sd_y_all sd_evic_all

**************************
*Regression specification*
**************************
global cluster_ols="judgexyear"
global cluster_iv="judgexyear"
global stem = ""
global instrument="loo_vc_ivsamp100"
global fe="districtxyearxquarter"

*Outcomes
local outcomes_main = "earn anyearn_allst"

*Define controls
macro define controls="noprior noprior_miss joint ad_damnum ageatcase ageatcase_sq ageatcase_cub fem black white fem_black fem_white hispanic fem_hisp fem_miss black_miss hispanic_miss nbrhd_pov_rate_5yr nbrhd_med_rent_5yr nbrhd_pov_rate_5yr_miss nbrhd_med_rent_5yr_miss"

*Define lagged dependant variable controls
global earn_lags = "earn_b8to1 earn_b8to1_miss earn_b1 earn_b1_miss earn_b2 earn_b2_miss earn_b3 earn_b3_miss earn_b4 earn_b4_miss "
global anyearn_allst_lags = "anyearn_allst_b8to1 anyearn_allst_b8to1_miss anyearn_allst_b1 anyearn_allst_b1_miss anyearn_allst_b2 anyearn_allst_b2_miss anyearn_allst_b3 anyearn_allst_b3_miss anyearn_allst_b4 anyearn_allst_b4_miss"
global anyoutstate_lags = "anyoutstate_b8to1 anyoutstate_b8to1_miss anyoutstate_b1 anyoutstate_b1_miss anyoutstate_b2 anyoutstate_b2_miss anyoutstate_b3 anyoutstate_b3_miss anyoutstate_b4 anyoutstate_b4_miss"
global b1to8_lags = "earn_b16to9 earn_b9 earn_b10 earn_b11 earn_b12 earn_b16to9_miss earn_b9_miss earn_b10_miss earn_b11_miss earn_b12_miss      anyearn_allst_b16to9 anyearn_allst_b9 anyearn_allst_b10 anyearn_allst_b11 anyearn_allst_b12 anyearn_allst_b16to9_miss anyearn_allst_b9_miss anyearn_allst_b10_miss anyearn_allst_b11_miss anyearn_allst_b12_miss"
global b1to4_lags = "earn_b12to5 earn_b5 earn_b6 earn_b7 earn_b8 earn_b12to5_miss earn_b5_miss earn_b6_miss earn_b7_miss earn_b8_miss        anyearn_allst_b12to5 anyearn_allst_b5 anyearn_allst_b6 anyearn_allst_b7 anyearn_allst_b8 anyearn_allst_b12to5_miss anyearn_allst_b5_miss anyearn_allst_b6_miss anyearn_allst_b7_miss anyearn_allst_b8_miss"
global b9to12_lags = "earn_b20to13 earn_b13 earn_b14 earn_b15 earn_b16 earn_b20to13_miss earn_b13_miss earn_b14_miss earn_b15_miss earn_b16_miss       anyearn_allst_b20to13 anyearn_allst_b13 anyearn_allst_b14 anyearn_allst_b15 anyearn_allst_b16 anyearn_allst_b20to13_miss anyearn_allst_b13_miss anyearn_allst_b14_miss anyearn_allst_b15_miss anyearn_allst_b16_miss"

***********************
* OLS/ IV REGRESSIONS *
***********************
cap gen all=1
gen sample_restriction =.

****** Great Recession Years: 2008-2012 ******
gen gr_years=(case_year>=2008 & case_year<=2012)

qui foreach pop in "all"   {
	foreach popval of numlist 0 1 {
		if "`pop'"~="all" | `popval'~=0	{	
		foreach grval of numlist 0 1 {
				foreach var of varlist earn  {
					foreach rng in "1_4" "5_8"  {
					
					nois di " ON outcome: `var', pop: `pop'=`popval', time period: `rng' "
					
					if (inlist("`rng'", "b4to1","b8to5","b8to1") & "`pop'" != "all") continue

					*Set lags to missing if outcome window is a per-period
					if ("`rng'"=="1_4" | "`rng'"=="1_8" | "`rng'"=="5_8" | "`rng'"=="5_12" | "`rng'"=="9_12" | "`rng'"=="9_16" | "`rng'"=="13_16" | "`rng'"=="13_20" | "`rng'"=="17_20" | "`rng'"=="17_24") local lags="$earn_lags $anyearn_allst_lags"
					if ("`rng'"=="b8to1" | "`rng'"=="b8to5" ) local lags=" $b1to8_lags "	
					if ( "`rng'"=="b4to1" ) local lags=" $b1to4_lags "		
					if ("`rng'"=="b12to9") local lags=" $b9to12_lags "
					
					*Restricting sample across regressions so obs are the same across specs
					ivregress 2sls `var'_`rng' i.$fe $controls (evicted=$instrument) if  gr_years==`grval' & `pop'==`popval', r cluster($cluster_iv) 
					replace sample_restriction = e(sample)
					
					*Non-evict mean
					sum `var'_`rng'   if sample_restriction==1 & evicted==0
					local y_base_mean=r(mean)
					local y_base_sd= r(sd)
					unique pik   if sample_restriction==1 & evicted==0 
					local y_base_n= r(sum)
					
					local sd_y_all = ""
					local sd_evic_all = ""
					local label="Pop: `pop'=`popval', Years: Great Recession=`grval', Outcome: `var', Outcome Window: `rng'"
					frame post results ("`label'")  (.) (.) (.) (`y_base_mean') (`y_base_sd') (`y_base_n') (.) (.)
				     
					*OLS with additional controls
					reg  `var'_`rng' evicted $controls `lags' i.$fe   if sample_restriction == 1, r cluster($cluster_ols)
					noi disp e(N)
					local b_evicted= _b[evicted]
					local se_evicted=_se[evicted] 
					local n_obs=e(N)
					local label="Pop: `pop'=`popval', Years: Great Recession=`grval', Outcome: `var', Outcome Window: `rng', Specification: OLS with additional controls" 	
					frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.) 
					
					*IV: Baseline with additional controls
					ivregress 2sls `var'_`rng' $controls `lags' i.$fe  (evicted=$instrument)   if sample_restriction == 1, r cluster($cluster_iv) 
					noi disp e(N)
					local b_evicted= _b[evicted]
					local se_evicted=_se[evicted] 
					local n_obs=e(N)
					local label="Pop: `pop'=`popval', Years: Great Recession=`grval', Outcome: `var', Outcome Window: `rng', Specification: IV with additional controls" 	
					frame post results ("`label'")  (`b_evicted') (`se_evicted')  (`n_obs')  (.) (.) (.) (.) (.)	
					
					}
				}
			}
		}
	}
}

frame results: export delimited using "${Inputs}/Cook_lehd_ivols_gr.csv", replace 
